﻿using Domain.IRepositorys;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.App
{
    public class PushApplication
    {
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public IGeTuiRelationRepository geTuiRelationRepository { get; set; }
        public ILogger<PushApplication> Logger { get; set; }

        public bool BindGeTuiRelation(int uid, string geTuiID)
        {
            if (string.IsNullOrWhiteSpace(geTuiID)) throw new BizException("个推geTuiID参数不能为空!");

            var bind = geTuiRelationRepository.Entities.FirstOrDefault(i => i.UserID == uid);

            if (bind != null)
            {
                if (geTuiID.Equals(bind.GeTuiID)) return true;

                bind.GeTuiID = geTuiID;
                bind.UT = DateTime.Now;

                geTuiRelationRepository.Update(bind);
            }
            else
            {
                bind = new Domain.Model.GeTuiRelationAggregate();

                bind.CT = bind.UT = DateTime.Now;
                bind.UserID = uid;
                bind.GeTuiID = geTuiID;

                geTuiRelationRepository.Insert(bind);
            }

            return efUnitOfWork.Commit() > 0;
        }
    }
}
